package com.bzchao.sptest.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

/**
 * @author 陈光超   2020/11/20 18:20
 */
@Slf4j
@RestController
@RequestMapping("/")
public class MainController {
    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @Autowired
    private TestRestController testRestController;

    @RequestMapping
    public Object main() {
        return "hello";
    }

    @RequestMapping("/addRest")
    public void addRest(String addr) throws NoSuchMethodException {
        log.info("新增rest接口：{}", addr);
        RequestMappingInfo requestMappingInfo = RequestMappingInfo.paths(addr).methods(RequestMethod.GET, RequestMethod.POST).build();
        //测试成功，Controller上的RequestMapping注解无效
        requestMappingHandlerMapping.registerMapping(requestMappingInfo, testRestController, testRestController.getClass().getMethod("testRest", String.class));

    }

}
